package net.kong.sns.rest.client;

import java.io.InputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author <a href="mailto:lanhuai@gmail.com">Ning Yubin</a>
 * @version $Id: AppKeysFile.java,v 1.2 2009/07/29 09:05:37 yongkun Exp $
 */
public class AppKeysFile implements AppKeys {
    private String apiKey;
    private String apiSecret;

    protected AppKeysFile() {
    }

    protected AppKeysFile(InputStream inputStream) throws IOException {
        load(inputStream);
    }
    protected void load(InputStream inputStream) throws IOException {
        if (inputStream != null) {
            Properties properties = new Properties();
            properties.load(inputStream);
            extractProperties(properties);
        }
        else {
            extractProperties(System.getProperties());
        }
    }

    protected void extractProperties(Properties properties) {
        apiKey = getRequiredProperty(properties, "apiKey");
        apiSecret = getRequiredProperty(properties, "apiSecret");
    }

    protected static String getRequiredProperty(Properties properties, String key) {
        String value = properties.getProperty(key);
        if (value == null || value.trim().length() == 0) {
            throw new AssertionError("Invalid kong_app configuration: " + key + " not specified");
        }
        return value;
    }

    public String getApiKey() {
        return apiKey;
    }

    public String getApiSecret() {
        return apiSecret;
    }

    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }

    public void setApiSecret(String apiSecret) {
        this.apiSecret = apiSecret;
    }
}
